package com.zgm.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by 张公铭 on 2017/6/29 0029.
 * 反射对象工具类
 */
public class ReflexObjectUtil
{
    /**
     * 单个对象的所有键值
     */
    public static Map<String, Object> getKeyAndValue(Object obj)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        // 得到类对象
        Class userCla = (Class) obj.getClass();
        /* 得到类中的所有属性集合 */
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++)
        {
            Field f = fs[i];
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = new Object();
            try
            {
                val = f.get(obj);
                // 得到此属性的值
                map.put(f.getName(), val);// 设置键值
            } catch (IllegalArgumentException e)
            {
                e.printStackTrace();
            } catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * 单个对象的所有数据类型与值
     */
    public static Map<Integer, Object> getTypeAndValue(Object obj)
    {
        Map<Integer, Object> map = new HashMap<>();
        // 得到类对象
        Class userCla = (Class) obj.getClass();
        /* 得到类中的所有属性集合 */
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++)
        {
            Field f = fs[i];
            Class cls = f.getType();
            int key = 0;
            if (cls == String.class)
            {
                key = 1;
            } else if (cls == Integer.class)
            {
                key = 2;
            } else if (cls == int.class)
            {
                key = 3;

            } else if (cls == long.class || cls == Long.class)
            {
                key = 4;
            } else if (cls == short.class || cls == Short.class)
            {
                key = 5;
            } else if (cls == float.class || cls == Float.class)
            {
                key = 6;
            } else if (cls == double.class || cls == Double.class)
            {
                key = 7;
            }
            f.setAccessible(true); // 设置些属性是可以访问的
            try
            {
                Object val = f.get(obj);
                if (val == null)
                {
                    map.put(key, "");
                } else
                {
                    // 得到此属性的值
                    map.put(key, val);// 设置键值
                }
            } catch (IllegalArgumentException e)
            {
                e.printStackTrace();
            } catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static List<Object> getValues(Object obj)
    {
        List<Object> list = new ArrayList<>();
        // 得到类对象
        Class cls = (Class) obj.getClass();
        Method[] methods = cls.getMethods();
        for(int i = 0; i < methods.length; i++){
            Method method = methods[i];
            if(method.getName().startsWith("get") && !method.getName().equals("getClass")){
                try
                {
                    list.add(method.invoke(obj));
                } catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                } catch (InvocationTargetException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
